﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Linq;
using System.Collections.Generic;


using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_GoodsPart。
    /// </summary>
    public class GoodsPartsDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="goodsIds"></param>
        public void Add(int parentId, int[] goodsIds)
        {
            using (Entities db = new Entities())
            {
                var q = db.GoodsParts.Where(x => x.ParentId == parentId);
                q.ToList().ForEach(x => db.GoodsParts.Remove(x));

                foreach (var v in goodsIds)
                {
                    db.GoodsParts.Add(new GoodsPart() { ParentId = parentId, GoodsId = v });
                }
                db.SaveChangesAsync().Wait();
            }
            
        }
        #endregion

        #region 捆绑销售商品
        /// <summary>
        /// 捆绑销售商品
        /// </summary>
        /// <param name="GoodsID"></param>
        /// <returns></returns>
        public IList<Goods> GetLists(int GoodsID)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Goods
                        join y in db.GoodsParts on x.Id equals y.GoodsId
                        where y.ParentId == GoodsID
                        select x;
                return q.ToList();
            }
        }

        #endregion
    }
}